<?php
namespace Api\Controller;
class LearningCenterController extends ApiController{
    
    /**
     * 教程列表
     * @param int page
     * @param int pagesize
     * @param string type
     */
    public function getList(){
        $page=I('page',1);
        $pagesize=I('pagesize',8);
        $page<=0 && $page=1;
        $pagesize <=0 && $pagesize=8;
        $offset=($page-1)*$pagesize;
        
        params_exist(array('type'));
        $type=I('type');
        $map=array();
        $fields=array();
        switch ($type){
            case 'doc':
                $fields=array('id,title,type,create_time,link');
                $map['type']='doc';
                break;
            case 'video':
                $fields=array('id,title,type,create_time,resources_link,link,movie');
                $map['type']='video';
                break;
            default :
                $fields=array('id,title,type,create_time,link');
                $map['type']='doc';
                break;
        }
        
        $M_l_c = M('learning_center');
        $total=$M_l_c->where($map)->count();
        $list=$M_l_c->where($map)->limit($offset,$pagesize)->field($fields)->select();
        foreach($list as &$item){
            if($item['movie']){
                $item['movie_path'] = mb_substr($_SERVER["HTTP_REFERER"],
                0, strrpos($_SERVER["HTTP_REFERER"], "/")) . '/Uploads/Movie/' . get_movie($item['movie'], 'savepath')
                . get_movie($item['movie'], 'savename');
                $item['movie_mime'] = get_movie($item['movie'], 'mime');
                
                $item['thumb_cover_id']=get_movie($item['movie'],'thumb_cover_id');
                $item['thumb_cover_path']=get_url( $item['thumb_cover_id']);
            }
        }
        $result['total']=$total;
        $result['result']=$list;
        $list ? response(1,$result): response(-2);
    }
    
    /**
     * 获取详细
     * @param int id
     */
    public function getDetail() {
        params_exist(array('id'));
        $id=I('id');
        $M_l_c = M('learning_center');
        $info=$M_l_c->find($id);
        
    if($info['movie']){
                $info['movie_path'] = mb_substr($_SERVER["HTTP_REFERER"],
                0, strrpos($_SERVER["HTTP_REFERER"], "/")) . '/Uploads/Movie/' . get_movie($info['movie'], 'savepath')
                . get_movie($info['movie'], 'savename');
                $info['movie_mime'] = get_movie($info['movie'], 'mime');
                
                $info['thumb_cover_id']=get_movie($info['movie'],'thumb_cover_id');
                $info['thumb_cover_path']=get_url( $info['thumb_cover_id']);
      }
        
        empty($info) && response(-2,'教程不存在');
        response(1,$info);
    }
}
?>